package pers.lx.crm.service.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.lx.crm.base.common.pigeon.Result;
import pers.lx.crm.base.constant.ErrorConstant;
import pers.lx.crm.base.util.ResultUtil;
import pers.lx.crm.dao.user.UserDao;
import pers.lx.crm.entiy.user.UserDO;
import pers.lx.crm.service.user.UserService;

import java.util.List;

/**
 * Created by lxl on 2017/2/10.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao ;

    @Override
    public UserDO getUserByUsername(String username) {
        return userDao.selectByUsername(username);
    }

    @Override
    public Result getUsers(UserDO userDO) {
        Result result = new Result() ;
        try {
            userDO.setList(userDao.selectUserListPage(userDO));
            return ResultUtil.getPageResult(userDO, result);
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setError(ErrorConstant.PARAMETER_NULL);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result ;
    }

    @Override
    public Result addUser(UserDO userDO) {
        Result result = new Result() ;
        try {
            userDao.insertUser(userDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Override
    public Result updateUser(UserDO userDO) {
        Result result = new Result() ;
        try {
            userDao.updateUser(userDO);
        } catch (Exception e){
            e.printStackTrace();
            result.setError(ErrorConstant.UNKNOWN_ERROR);
        }
        return result;
    }

    @Transactional
    @Override
    public Result removeUsers(List<UserDO> userDOs) {
        Result result = new Result() ;
        for (int i = 0 ; i < userDOs.size() ; i++){
            userDao.deleteUserByUId(userDOs.get(i));
        }
        return result;
    }
}
